<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="!jumpLoading">
      <h3 class="title">{{pageTitle}}</h3>
      <el-form-item prop="userName">
        <el-input
          v-model="loginForm.userName"
          type="text"
          auto-complete="off"
          placeholder="请输入用户名"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="userPassword">
        <el-input
          v-model="loginForm.userPassword"
          type="password"
          auto-complete="off"
          show-password
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="imgCode">
        <el-input
          v-model="loginForm.imgCode"
          auto-complete="off"
          placeholder="请输入验证码"
          style="width: 63%"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code" @click="getCode">
          <img v-show="codeUrl" :src="codeUrl" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
	  <div id="loader-wrapper" v-if="jumpLoading">
		  <div id="loader"></div>
		  <div class="loader-section section-left"></div>
		  <div class="loader-section section-right"></div>
		  <div class="load_title">登录中...</div>
	  </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024 All Rights Reserved.</span>
    </div>

  </div>
</template>

<script>
  import { md5 } from '@/utils/sign'
  import { getBrowerInfo,isEmptyStr } from  '@/utils/util'


  import { getCodeImg } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'


  export default {
    name: "Login",
    data() {
      return {
        pageTitle: process.env.VUE_APP_TITLE,
        codeUrl: '',
        loginForm: {
          loginType: 'USERNAME',
          equipmentType: 'pc',
          browser: getBrowerInfo().browserType,
          loginKey: '',  // 验证码的loginKey
          systemVersion: '',
          systemCode: '',
          systemName: 'Admin',
          category: 'EASY_ARITHMETIC',
          userType: '10',
          imgCode: '',
          userName: '',
          userPassword: '',
        },
        loginRules: {
          userName: [
            { required: true, trigger: 'blur', message: '请输入用户名' }
          ],
          userPassword: [
            { required: true, trigger: 'blur', message: "请输入密码" }
          ],
          imgCode: [
            { required: true, trigger: 'blur', message: "请输入验证码" }
          ]
        },
        loading: false,
        jumpLoading: false,
        jumpFaile: false,
        redirect: undefined,
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode()
    },
	  mounted() {
		const  authCode = this.$route.query.auth_code;
		  console.log("authCode", authCode)
		if (!isEmptyStr(authCode)) {
			console.log("!==authCode", authCode)
			this.jumpLoading = true;
			this.onSignIn(authCode);
		}
	  },
	  methods: {
		  async onSignIn(authCode){
			  console.log("authCode   ",authCode)
			  let userInfoObj = {
				  authCode : authCode
			  }
			  await this.$http.postCrypto('ykzGetTokenPcBigScreen', userInfoObj).then(async (res) => {
				  console.log("res",res)
				  if (res.code == 0) {
					  const { result } = res
					  // console.log(result)
					  this.$store.dispatch("Login", result)
					  this.jumpLoading = false;
					  this.$router.push({path: this.redirect || "/"}).catch(() => { })
				  } else {
					  console.log("faile")
					  this.jumpLoading = false;
					  this.jumpFaile = true;
					  this.alertWarningFun();
				  }
			  }).catch(er => {
				  this.jumpLoading = false;
				  this.jumpFaile = true;
				  this.alertWarningFun();
				  console.log("登录失败！",er);
				  console.log("faile")
			  });
		  },
		  alertWarningFun(){
			  this.$alert('尊敬的用户，您没有本应用访问权限。', '提示', {
				  confirmButtonText: '确定',
				  callback: action => {
					  // this.$message({
						//   type: 'info',
						//   message: `action: ${ action }`
					  // });
				  }
			  });
		  },
      // 获取验证码
      getCode() {
        this.$http.postCrypto('login0004', {category: 'EASY_ARITHMETIC'}).then(res=>{
          const { graphicImageBase64, loginKey } = res.result
          this.codeUrl = graphicImageBase64
          this.loginForm.loginKey = loginKey
        }).catch(err=>{

          console.log(err, 'err')
        })
      },
      // 登录
      async handleLogin() {
        let valid = await this.$refs.loginForm.validate()
        if (!valid) return false
        this.loading = true
        const { userPassword } = this.loginForm
        let params = {...this.loginForm, userPassword: md5(userPassword)}
        try {
          let loginRes = await this.$http.postCrypto('login0001', params)
          const { result } = loginRes
          console.log(result)
          this.$store.dispatch("Login", result)
          this.loading = false
          this.$router.push({path: this.redirect || "/"}).catch(() => { })
        } catch (e) {
          this.loading = false
          this.getCode()
        }
      }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .icon-box {
    overflow: hidden; padding-top: 18px; display: flex; align-items: center;
    .cursor {
      cursor: pointer;
    }
    .grey {
      filter: drop-shadow(#ccc 80px 0);
      transform: translateX(-80px); margin-right: 10px;
    }
    .alipay-icon { font-size: 27px; }
    .wechat1-icon { font-size: 24px; }
    .hyt-icon { font-size: 24px; }
  }
  .iframe { position: absolute; width: 100%; height: 100%; z-index: 33; }
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background.jpg");
    background-size: cover;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .el-login-footer {
    display: none;
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 36px;
    width: 115px;
  }


  #loader-wrapper {
	  position: fixed;
	  top: 0;
	  left: 0;
	  width: 100%;
	  height: 100%;
	  z-index: 999999;
  }

  #loader {
	  display: block;
	  position: relative;
	  left: 50%;
	  top: 50%;
	  width: 150px;
	  height: 150px;
	  margin: -75px 0 0 -75px;
	  border-radius: 50%;
	  border: 3px solid transparent;
	  border-top-color: #FFF;
	  -webkit-animation: spin 2s linear infinite;
	  -ms-animation: spin 2s linear infinite;
	  -moz-animation: spin 2s linear infinite;
	  -o-animation: spin 2s linear infinite;
	  animation: spin 2s linear infinite;
	  z-index: 1001;
  }

  #loader:before {
	  content: "";
	  position: absolute;
	  top: 5px;
	  left: 5px;
	  right: 5px;
	  bottom: 5px;
	  border-radius: 50%;
	  border: 3px solid transparent;
	  border-top-color: #FFF;
	  -webkit-animation: spin 3s linear infinite;
	  -moz-animation: spin 3s linear infinite;
	  -o-animation: spin 3s linear infinite;
	  -ms-animation: spin 3s linear infinite;
	  animation: spin 3s linear infinite;
  }

  #loader:after {
	  content: "";
	  position: absolute;
	  top: 15px;
	  left: 15px;
	  right: 15px;
	  bottom: 15px;
	  border-radius: 50%;
	  border: 3px solid transparent;
	  border-top-color: #FFF;
	  -moz-animation: spin 1.5s linear infinite;
	  -o-animation: spin 1.5s linear infinite;
	  -ms-animation: spin 1.5s linear infinite;
	  -webkit-animation: spin 1.5s linear infinite;
	  animation: spin 1.5s linear infinite;
  }


  @-webkit-keyframes spin {
	  0% {
		  -webkit-transform: rotate(0deg);
		  -ms-transform: rotate(0deg);
		  transform: rotate(0deg);
	  }
	  100% {
		  -webkit-transform: rotate(360deg);
		  -ms-transform: rotate(360deg);
		  transform: rotate(360deg);
	  }
  }

  @keyframes spin {
	  0% {
		  -webkit-transform: rotate(0deg);
		  -ms-transform: rotate(0deg);
		  transform: rotate(0deg);
	  }
	  100% {
		  -webkit-transform: rotate(360deg);
		  -ms-transform: rotate(360deg);
		  transform: rotate(360deg);
	  }
  }


  #loader-wrapper .loader-section {
	  position: fixed;
	  top: 0;
	  width: 51%;
	  height: 100%;
	  background: #7171C6;
	  z-index: 1000;
	  -webkit-transform: translateX(0);
	  -ms-transform: translateX(0);
	  transform: translateX(0);
  }

  #loader-wrapper .loader-section.section-left {
	  left: 0;
  }

  #loader-wrapper .loader-section.section-right {
	  right: 0;
  }


  .loaded #loader-wrapper .loader-section.section-left {
	  -webkit-transform: translateX(-100%);
	  -ms-transform: translateX(-100%);
	  transform: translateX(-100%);
	  -webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
	  transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
  }

  .loaded #loader-wrapper .loader-section.section-right {
	  -webkit-transform: translateX(100%);
	  -ms-transform: translateX(100%);
	  transform: translateX(100%);
	  -webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
	  transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1.000);
  }

  .loaded #loader {
	  opacity: 0;
	  -webkit-transition: all 0.3s ease-out;
	  transition: all 0.3s ease-out;
  }

  .loaded #loader-wrapper {
	  visibility: hidden;
	  -webkit-transform: translateY(-100%);
	  -ms-transform: translateY(-100%);
	  transform: translateY(-100%);
	  -webkit-transition: all 0.3s 1s ease-out;
	  transition: all 0.3s 1s ease-out;
  }

  .no-js #loader-wrapper {
	  display: none;
  }

  .no-js h1 {
	  color: #222222;
  }

  #loader-wrapper .load_title {
	  font-family: 'Open Sans';
	  color: #FFF;
	  font-size: 19px;
	  width: 100%;
	  text-align: center;
	  z-index: 9999999999999;
	  position: absolute;
	  top: 60%;
	  opacity: 1;
	  line-height: 30px;
  }

  #loader-wrapper .load_title span {
	  font-weight: normal;
	  font-style: italic;
	  font-size: 13px;
	  color: #FFF;
	  opacity: 0.5;
  }
</style>

